package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author mqx
 * @date 2020-8-3 14:04:29
 */
@RestController
@RequestMapping("api/list")
public class ListApiController {

    // 创建index,type 使用es的类 ElasticsearchRestTemplate
    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private SearchService searchService;

    @GetMapping("inner/createIndex")
    public Result createIndex(){
        // 调用类中方法自动创建
        restTemplate.createIndex(Goods.class);
        restTemplate.putMapping(Goods.class);

        return Result.ok();
    }

    // 制作商品上架的控制器！
    @GetMapping("inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId){
        searchService.upperGoods(skuId);
        return Result.ok();
    }
    // 制作商品下架的控制器！
    @GetMapping("inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        searchService.lowerGoods(skuId);
        return Result.ok();
    }

    // 商品的热度排名
    @GetMapping("inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable Long skuId){
        searchService.incrHotScore(skuId);
        return Result.ok();
    }

    // 用户访问检索时的流程：
    // https://gmall.list.com/list.html?category3Id=61
    // web-all {list.html} 通过feign 远程调用，service-list 中查询结果发布到feign。
    // 与前台接口协商一致！
    // 参数传递
    @PostMapping
    public Result getList(@RequestBody SearchParam searchParam) throws Exception{
        // 查询数据
        SearchResponseVo responseVo = searchService.search(searchParam);
        // 返回数据
        return Result.ok(responseVo);
    }

}
